package bb.lanxing.fragment.routebook;

import java.util.ArrayList;
import java.util.List;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.immersionbar.ImmersionBar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.core.internal.view.SupportMenu;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import bb.lanxing.adapter.mine.MyPagerAdapter;
import bb.lanxing.R;
import bb.lanxing.activity.routebook.RouteBookSearchActivity;
import bb.lanxing.device.thirdpart.gpx.GPXImportActivity;
import bb.lanxing.fragment.base.BaseTabFragment;
import bb.lanxing.util.BusProvider;
import bb.lanxing.util.RouteBookUtil;

public class RouteBookFragment extends BaseTabFragment {
    public static final String EXTRA_SELECTION_LUSHU = "extra_selection_lushu";
    public static final String FROM_SPORT_MAP = "from_sport_map";
    public static final String INTENT_TYPE = "intent_type";
    public static final int INTENT_TYPE_NORMAL = 1;
    public static final int INTENT_TYPE_SHARE = 2;
    private static final int CREATE_LUSHU_REQUEST = 101;
    private final List<Fragment> fragmentList = new ArrayList<>();
    private final ActivityResultLauncher<String> getGpxLauncher =
            registerForActivityResult(new ActivityResultContracts.GetContent(), uri -> {
                if (uri != null) {
                    GPXImportActivity.Companion.launch(RouteBookFragment.this.requireContext(), uri);
                }
            });
    private boolean fromSportMap = false;
    private int intentType = INTENT_TYPE_NORMAL;
    private int currentIndex = 0;
    private boolean isSelectionLushu = false;

    public static RouteBookFragment newInstance() {
        return new RouteBookFragment();
    }

    public static RouteBookFragment newInstance(boolean z, int i, boolean z2) {
        RouteBookFragment routeBookFragment = new RouteBookFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(FROM_SPORT_MAP, z);
        bundle.putInt(INTENT_TYPE, i);
        bundle.putBoolean(EXTRA_SELECTION_LUSHU, z2);
        routeBookFragment.setArguments(bundle);
        return routeBookFragment;
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        BusProvider.getInstance().register(this);
        if (getArguments() != null) {
            this.fromSportMap = getArguments().getBoolean(FROM_SPORT_MAP);
            this.intentType = getArguments().getInt(INTENT_TYPE);
            this.isSelectionLushu = getArguments().getBoolean(EXTRA_SELECTION_LUSHU, false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusProvider.getInstance().unregister(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_lushu, viewGroup, false);
        bindView(inflate);
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        ImmersionBar.with(this).transparentStatusBar().statusBarDarkFont(true).init();
    }

    private void onSearchViewClick(View view) {
        startActivity(new Intent(getActivity(), RouteBookSearchActivity.class));
    }

    private void onCreateViewClick(View view) {
        RouteBookUtil.showRouteBookCreateDialog(getContext(), this, CREATE_LUSHU_REQUEST, getGpxLauncher);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        List<Fragment> fragments;
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == CREATE_LUSHU_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                RouteBookUtil.showDialogForCreatedLushu(getContext());
            }
        } else if ((requestCode & SupportMenu.CATEGORY_MASK) == 0
                && (fragments = getChildFragmentManager().getFragments()) != null && !fragments.isEmpty()) {
            for (Fragment fragment : fragments) {
                fragment.onActivityResult(resultCode, resultCode, intent);
            }
        }
    }

    private void bindView(View view) {
        view.findViewById(R.id.createView).setOnClickListener(this::onCreateViewClick);
        view.findViewById(R.id.menu_lushu_search).setOnClickListener(this::onSearchViewClick);
        ViewPager2 viewPager2 = view.findViewById(R.id.pager2);
        TabLayout tabLayout = view.findViewById(R.id.tabLayout);
        Bundle bundle = new Bundle();
        bundle.putInt(INTENT_TYPE, this.intentType);
        bundle.putBoolean(FROM_SPORT_MAP, this.fromSportMap);
        Fragment recommendationLushuFragment = new RecommendRouteBookFragment();
        recommendationLushuFragment.setArguments(bundle);
        fragmentList.add(recommendationLushuFragment);
        Fragment myLushuFragment = new MyRouteBookFragment();
        myLushuFragment.setArguments(bundle);
        fragmentList.add(myLushuFragment);
        Fragment nearByLushuListFragment = new NearByRouteBookListFragment();
        nearByLushuListFragment.setArguments(bundle);
        fragmentList.add(nearByLushuListFragment);
        MyPagerAdapter adapter = new MyPagerAdapter(requireActivity(), fragmentList);
        viewPager2.setAdapter(adapter);
        viewPager2.setUserInputEnabled(false);
        if (isSelectionLushu) {
            currentIndex = 1;
        }
        viewPager2.setCurrentItem(currentIndex);
        new TabLayoutMediator(tabLayout, viewPager2, true, (tab, i) -> {
            currentIndex = i;
            tab.setText(currentIndex == 0 ? R.string.str_lushu_recommended
                    : currentIndex == 1 ? R.string.str_lushu_my : R.string.str_lushu_nearby);
        }).attach();
    }
}
